package com.qqtech.qquan.hi.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.enums.BizzLevel2Enum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.activity.enums.ActivityBaseTypeDictEnum;
import com.qqtech.qquan.activity.model.ActivityBaseVo;
import com.qqtech.qquan.activity.model.ActivitySignVo;
import com.qqtech.qquan.activity.service.ActivityBaseService;
import com.qqtech.qquan.activity.service.ActivitySignService;
import com.qqtech.qquan.circle.enums.CircleTopicBizzTypeDictEnum;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.community.service.CommunityJoinService;
import com.qqtech.qquan.community.service.CommunityMessageService;
import com.qqtech.qquan.community.service.CommunitySignService;
import com.qqtech.qquan.hi.enums.HiRuleClassifyDictEnum;
import com.qqtech.qquan.hi.enums.HiRuleTaskTypeDictEnum;
import com.qqtech.qquan.hi.model.HiRuleQuery;
import com.qqtech.qquan.hi.model.HiRuleVo;
import com.qqtech.qquan.hi.model.HiUserTask;
import com.qqtech.qquan.hi.model.HiUserTaskQuery;
import com.qqtech.qquan.hi.model.HiUserTaskVo;
import com.qqtech.qquan.hi.service.HiCircleService;
import com.qqtech.qquan.hi.service.HiCommunityService;
import com.qqtech.qquan.hi.service.HiOrderService;
import com.qqtech.qquan.hi.service.HiRuleService;
import com.qqtech.qquan.hi.service.HiShopService;
import com.qqtech.qquan.hi.service.HiUserService;
import com.qqtech.qquan.hi.service.HiUserTaskService;
import com.qqtech.qquan.order.enums.OrderBasePayStatusDictEnum;
import com.qqtech.qquan.order.model.OrderBaseQuery;
import com.qqtech.qquan.order.model.OrderBaseVo;
import com.qqtech.qquan.order.service.OrderBaseService;
import com.qqtech.qquan.pub.model.PubCommentVo;
import com.qqtech.qquan.pub.model.PubConcernVo;
import com.qqtech.qquan.pub.model.PubFeedbackQuery;
import com.qqtech.qquan.pub.model.PubFeedbackVo;
import com.qqtech.qquan.pub.model.PubPraiseVo;
import com.qqtech.qquan.pub.service.PubCommentService;
import com.qqtech.qquan.pub.service.PubConcernService;
import com.qqtech.qquan.pub.service.PubFeedbackService;
import com.qqtech.qquan.pub.service.PubPraiseService;
import com.qqtech.qquan.sys.controller.BaseAgentController;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserFriendService;
import com.qqtech.qquan.vote.model.VoteResultVo;
import com.qqtech.qquan.vote.service.VoteBaseService;
import com.qqtech.qquan.vote.service.VoteResultService;

@Controller
@RequestMapping(value = "/hi/user/task")
public class HiUserTaskController extends BaseAgentController {

	@Resource
	private HiUserTaskService hiUserTaskService;
	@Resource
	private HiRuleService hiRuleService;
	@Resource
	private HiUserService hiUserService;
	@Resource
	private HiCircleService hiCircleService;
	@Resource
	private HiCommunityService hiCommunityService;
	@Resource
	private HiOrderService hiOrderService;
	@Resource
	private HiShopService hiShopService;
	/*************************************/
	
	@Resource
	private UserBaseService userBaseService;
	@Resource
	private CommunityJoinService communityJoinService;
	@Resource
	private CommunitySignService communitySignService;
	@Resource
	private CommunityMessageService communityMessageService;
	@Resource
	private CircleBaseService circleBaseService;
	@Resource
	private CircleTopicService circleTopicService;
	@Resource
	private PubCommentService pubCommentService;
	@Resource
	private PubPraiseService pubPraiseService;
	@Resource
	private PubConcernService pubConcernService;
	@Resource
	private PubFeedbackService pubFeedbackService;
	@Resource
	private ActivitySignService activitySignService;
	@Resource
	private ActivityBaseService activityBaseService;
	
	@Resource
	private VoteBaseService voteBaseService;
	@Resource
	private VoteResultService voteResultService;
	@Resource
	private UserFriendService userFriendService;
	@Resource
	private OrderBaseService orderBaseService;
	
	@Override
	protected BaseService<HiUserTask> getBaseService() {
		return hiUserTaskService;
	}

	/**
	 * 同步老用户【新手任务、荣誉系统】
	 * @param classify
	 * @return
	 */
	@RequestMapping(value = "/fixUserTask4OldUser")
	@ResponseBody
	public Result fixUserTask4OldUser(Integer classify){
		StringBuffer msg = new StringBuffer();
		Result result = new Result();
		HiRuleQuery hiRuleQ = new HiRuleQuery();
		hiRuleQ.setTaskType(HiRuleTaskTypeDictEnum.NOVICE.getCode());//新手任务
		hiRuleQ.setStatus(YesNoEnum.YES.getCode());
		if(classify!=null){
			hiRuleQ.setClassify(classify);
		}else{
			result.addErr("请传入参数：classify的值,方式： ?classify=xx");
			return result;
		}
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.ASC, "id"));
		List<HiRuleVo> ruleList = hiRuleService.queryList(hiRuleQ, pageKit);
		HiUserTaskQuery taskQ = new HiUserTaskQuery();
		if(ruleList!=null && ruleList.size()>0){//for ruleId
			for(HiRuleVo rule : ruleList){
				int fixCount = 0;//该规则共找到已完成的用户数量
				Integer classifyId = rule.getClassify();
				Integer ruleId = rule.getId();
				String ruleName = rule.getName();
				
				taskQ.setRuleId(ruleId);//规则id
				taskQ.setIsFinish(YesNoEnum.NO.getCode());//未完成的任务
				List<HiUserTaskVo> taskList = hiUserTaskService.queryList(taskQ, pageKit);
				if(taskList!=null && taskList.size()>0){
					for(HiUserTaskVo taskVo : taskList){
						if(fixBuss4UserTask(taskVo.getUserId(),classifyId)){
							fixCount ++;
						}
					}
				}
				msg.append("规则"+ruleId+"-"+ruleName+"： 共用"+fixCount+"个老用户已完成,并同步荣誉系统.\n");
			}
		}
		
		result.put("message", msg.toString());		
		return result;
	}
	
	private boolean fixBuss4UserTask(Integer userId, Integer classifyId){
		boolean isFix = false;//是否已完成这项任务
		if(classifyId==HiRuleClassifyDictEnum.REGISTER.getCode()){//1注册
			hiUserService.register(userId);
			isFix = true;
		}else if(classifyId==HiRuleClassifyDictEnum.UPLOAD_USER_LOGO.getCode()){//2上传用户头像
			UserBaseVo user = userBaseService.getById(userId);
			if(user!=null && 
					StringUtil.isNotBlank(user.getLogo()) && 
					!user.getLogo().equalsIgnoreCase("/user/userLogin.png")){//是否有头象
				hiUserService.uploadLogo(userId);
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.UPDATE_NAME.getCode()){//3修改昵称
			UserBaseVo user = userBaseService.getById(userId);
			if(user!=null && 
					StringUtil.isNotBlank(user.getNick()) && 
					user.getNick().indexOf("**")!=0){//是否有呢称
				hiUserService.editNick(userId);
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.JOIN_COMMUNITY.getCode()){//4加入社区
			List<Integer> comIdList = communityJoinService.queryMyCommuntiyIds(userId);
			if(comIdList!=null && comIdList.size()>0){//是有加入社区
				hiCommunityService.join(userId, comIdList.get(0));
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.SIGN.getCode()){//5签到
			Integer communityId = communitySignService.myLastSignCommunity(userId);//用户最后签到的社区 id
			if(communityId!=null){//是否在社区签到过
				hiCommunityService.sign(userId, communityId);
				isFix = true;
			}
			
		}else if(classifyId==HiRuleClassifyDictEnum.JOIN_CIRCLE.getCode()){//6加入圈子
			List<CircleBaseVo> myJoinCircles = circleBaseService.queryMyJoinCircles(userId);
			if(myJoinCircles!=null && myJoinCircles.size()>0){//是否加入过圈子
				CircleBaseVo circle = myJoinCircles.get(0);
				if(circle!=null){
					hiCircleService.join(userId, circle.getId());
					isFix = true;
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.NEIGHBOURHOOD_DYNAMIC.getCode()){//7发布邻里动态
			List<CommunityMessageVo> messageList = communityMessageService.myLastSomeMessages(userId, 1);
			if(messageList!=null && messageList.size()>0){//是否发布过邻里动态
				CommunityMessageVo message = messageList.get(0);
				if(message!=null){
					hiCommunityService.addMessage(userId, message.getCommunityId());
					isFix = true;
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.CIRCLE_DYNAMIC.getCode()){//8发布圈子动态
			List<Integer> myTopicCicleIds = circleTopicService.queryMyTopicCircleIds(userId);
			if(myTopicCicleIds!=null && myTopicCicleIds.size()>0){//是否发布过圈子动态
				hiCircleService.addTopic(userId, myTopicCicleIds.get(0));
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.CIRCLE_ACTIVITY.getCode()){//9参与圈子活动
			List<ActivitySignVo> signList = activitySignService.myLastSomeSigns(userId, 1);
			if(signList!=null && signList.size()>0){//是否参与过圈子活动
				ActivityBaseVo activityVo = activityBaseService.getById(signList.get(0).getActivityId());
				if(activityVo!=null && activityVo.getType()==ActivityBaseTypeDictEnum.CIRCLE.getCode()){
					hiCircleService.inActivity(userId, activityVo.getBizzId());
					isFix = true;
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.CIRCLE_VOTE.getCode()){//10参与圈子投票
			List<VoteResultVo> voteResultList = voteResultService.myLastSomeVoteResults(userId, 1);
			if(voteResultList!=null && voteResultList.size()>0){//是否参与过圈子投票
				VoteResultVo result = voteResultList.get(0);
				Integer voteId = result.getVoteId();
				Integer topicId = circleTopicService.getTopicIdByBizzType(voteId, CircleTopicBizzTypeDictEnum.VOTE);
				if(topicId!=null){
					hiCircleService.addVoteResult(userId,topicId);
					isFix = true;
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.COMMUNITY_DYNAMIC_PRAISE.getCode()){//12点赞他人邻里动态
			List<PubPraiseVo> praiseList = pubPraiseService.myLastSomePraises(userId, 1, BizzLevel2Enum.COMMUNITY_MESSAGE);
			if(praiseList!=null && praiseList.size()>0){//是否点赞过他人邻里动态
				PubPraiseVo praiseVo = praiseList.get(0);
				if(praiseVo!=null && praiseVo.getBizzId()!=null){
					hiCommunityService.praiseMessage(userId, praiseVo.getBizzId());	
					isFix = true;
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.ADD_FRIEND.getCode()){//13添加好友
			List<Integer> friendIds = userFriendService.queryMyFriendIds(userId);
			if(friendIds!=null && friendIds.size()>0){//是否添加过好友
				hiUserService.addFriend(userId, friendIds.get(0));
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.COLLECT_SHOP.getCode()){//14收藏店铺
			List<PubConcernVo> concernList = pubConcernService.myLastSomeConcerns(userId, 1, BizzLevel2Enum.QSHOP_SHOP);
			if(concernList!=null && concernList.size()>0){//是否收藏过店铺
				PubConcernVo concernVo = concernList.get(0);
				if(concernVo!=null){
					hiShopService.collection(userId, concernVo.getBizzId());
					isFix = true;
				}
				
			}
		}else if(classifyId==HiRuleClassifyDictEnum.ORDER_SUCCESS.getCode()){//15平台下单成功
			
			OrderBaseQuery query = new OrderBaseQuery();
			query.setBuyerId(userId);
			query.setPayStatus(OrderBasePayStatusDictEnum.PAYED.getCode());
			Pageable pageKit = new PageKit(1,1, new PageOrder(Direction.DESC, "newAt"));
			List<OrderBaseVo> orderList = orderBaseService.queryList(query, pageKit);
			
			if(orderList!=null && orderList.size()>0){//是否平台下单成功
				hiOrderService.orderSuccess(userId, orderList.get(0).getId());
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.EVALUATE_ORDER.getCode()){//16评价订单
			PubFeedbackQuery query = new PubFeedbackQuery();
			query.setCreatorId(userId);
			query.setStatus(YesNoEnum.YES.getCode());
			Pageable pageKit = new PageKit(1,1, new PageOrder(Direction.DESC, "newAt"));
			List<PubFeedbackVo> feedBackList = pubFeedbackService.queryList(query, pageKit);
			if(feedBackList!=null && feedBackList.size()>0){//是否评价订单
				String orderNo = feedBackList.get(0).getOrderNo();
				if(StringUtil.isNotBlank(orderNo)){
					OrderBaseVo order = orderBaseService.getByOrderNo(orderNo);
					if(order!=null){
						hiOrderService.orderComment(userId, order.getId());
						isFix = true;
					}
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.CIRCLE_DYNAMIC_PRAISE.getCode()){//17点赞他人圈子动态
			List<PubPraiseVo> praiseList = pubPraiseService.myLastSomePraises(userId, 1, BizzLevel2Enum.CIRCLE_TOPIC);
			if(praiseList!=null && praiseList.size()>0){//是否点赞过他人圈子动态
				PubPraiseVo praiseVo = praiseList.get(0);
				if(praiseVo!=null && praiseVo.getBizzId()!=null){
					hiCircleService.praiseTopic(userId, praiseVo.getBizzId());	
					isFix = true;
				}
			}
		}else if(classifyId==HiRuleClassifyDictEnum.CIRCLE_DYNAMIC_COMMENT.getCode()){//18评论他人圈子动态
			List<PubCommentVo> commentList = pubCommentService.myLastSomeComments(userId, 1, BizzLevel2Enum.CIRCLE_TOPIC);
			if(commentList!=null && commentList.size()>0){//是否评论过他人圈子动态
				hiCircleService.commentTopic(userId, commentList.get(0).getBizzId());
				isFix = true;
			}
		}else if(classifyId==HiRuleClassifyDictEnum.COMMUNITY_DYNAMIC_COMMENT.getCode()){//19评论他人邻里动态
			List<PubCommentVo> commentList = pubCommentService.myLastSomeComments(userId, 1, BizzLevel2Enum.COMMUNITY_MESSAGE);
			if(commentList!=null && commentList.size()>0){//是否评论过他人邻里动态
				hiCommunityService.commentMessage(userId, commentList.get(0).getBizzId());
				isFix = true;
			}
		}
		
		return isFix;
	}
	
	
}
